# Create your views here.
from backend.models import *
from django.http import HttpResponse, Http404
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth.models import User
from django.template import RequestContext
from django.template import TemplateDoesNotExist
from django.views.defaults import page_not_found
import MySQLdb
import math
from django.contrib.auth import logout
import bookstore.settings
rootPath = bookstore.settings.ROOT_PATH
from django.http import HttpResponseRedirect
from django.contrib.auth import logout

var_back_category = ''
var_category_paging = ''
var_book = ''
var_book_paging = ''
var_book_category = ''
var_check = ''
var_user = ''
var_user_paging = ''
loged = ''
bookNameData = ''
catNameData = ''
var_book_head = ''
var_static = ''
var_detail_static = ''
var_title_static = ''
var_detail_setting = ''
var_title_setting = ''
var_integer = 0
var_setting = ''
def return_request(template, request):
    variables = RequestContext(request, {'rootPath':rootPath, 'var_back_category':var_back_category, 'var_category_paging':var_category_paging, 'var_book':var_book, 'var_book_paging':var_book_paging, 'var_book_category':var_book_category, 'var_check':var_check, 'var_user':var_user, 'var_user_paging':var_user_paging, 'loged':loged, 'bookNameData':bookNameData, 'catNameData':catNameData
    , 'var_book_head':var_book_head, 'var_static':var_static, 'var_detail_static':var_detail_static, 'var_title_static':var_title_static
    , 'var_integer':var_integer, 'var_setting':var_setting, 'var_detail_setting':var_detail_setting, 'var_title_setting':var_title_setting})
    output = template.render(variables)
    return HttpResponse(output)
    
def backend_page(request):
    if check_loged(request) == False:
        str = '%s/login' % (rootPath)
        return HttpResponseRedirect(str)
    template = get_template('backend/index.html')
    return return_request(template, request)

def book_page(request):
    template = get_template('backend/vBookModule.html')
    variables = Context({ 'rootPath': rootPath })
    output = template.render(variables)
    return HttpResponse(output)

def view_category(page):
    global var_back_category
    global var_category_paging
    var_back_category = ''
    var_category_paging = ''
    rows = getAllCategory()
    len = 0
    for row in rows:
        len += 1
    numberofpage = int(math.ceil(len / 5.0))
    j = -1
    for i in range ((page - 1) * 5, page * 5):
        j += 1
        if i >= len:
            break
        row = rows[i]
        if j % 2 == 1:
            var_back_category += '<tr class="alternate-row">\n\
                        '
        else:
            var_back_category += '<tr class="">\n\
                        '
        linkbook = rootPath + '/backend/book/view/category/%i' % (row[0])
        var_back_category += '\n\
                    <td>%s</td>\n\
                    <td>%s</td>\n\
                    <td><a href="%s">View Books</a></td>\n\
                    <td class="options-width">\n\
                    <a href="../edit/1 "title="Edit" class="icon-1 info-tooltip"></a>\n\
                    <a href="../delete/1?numRow=5"title="Delete" class="icon-2 info-tooltip"></a>\n\
                    </td>\n\
                </tr>\n\
                ' % (row[0], row[1], linkbook)
    previous = 1
    next = 1
    if (page == 1):
        previous = 1
    else:
        previous = page - 1
    if(page == numberofpage):
        next = page
    else:
        next = page + 1
    link = rootPath + '/backend/category/view/page01'
    link1 = rootPath + '/backend/category/view/page0%s' % (previous)
    link2 = rootPath + '/backend/category/view/page0%s' % (next)
    link3 = rootPath + '/backend/category/view/page0%s' % (numberofpage)
    var_category_paging = '<a href="%s" class="page-far-left"></a>\n\
                <a href="%s" class="page-left"></a>\n\
                <div id="page-info">Page <strong>%s</strong> / 4</div>\n\
                <a href="%s" class="page-right"></a>\n\
                <a href="%s" class="page-far-right"></a>\n\
                ' % (link, link1, page, link2, link3)
                
def back_category_page(request):
    return HttpResponseRedirect('page01')
    
def back_category_page_paging(request, page):
    currentpage = int(page)
    view_category(currentpage)
    template = get_template('backend/category.html')
    return return_request(template, request)

def view_book(page):
    global var_book
    global var_book_paging
    global var_book_head
    var_book = ''
    var_book_paging = ''
    rows = getAllBook()
    len = 0
    for row in rows:
        len += 1
    numberofpage = int(math.ceil(len / 5.0))
    var_book_head = '<tr>\n\
                        <th class="table-header-check table-header-repeat"><a href="">ID</a>\n\
                        </th>\n\
                        <th class="table-header-repeat line-left minwidth-1"><a\n\
                            href="">Name</a></th>\n\
                        <th class="table-header-repeat line-left"><a href="">Category</a>\n\
                        </th>\n\
                        <th class="table-header-repeat line-left"><a href="">Created</a>\n\
                        </th>\n\
                        <th class="table-header-options line-left"><span\n\
                            class="none-link">Options</span>\n\
                        </th>\n\
                    </tr>\n\
                    '
    j = -1                
    for i in range ((page - 1) * 5, page * 5):
        j += 1           
        if i >= len:
            break
        row = rows[i]
        if j % 2 == 1:
            var_book += '<tr class="alternate-row">\n\
                        '
        else:
            var_book += '<tr class="">\n\
                    '
        var_book += '\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td class="options-width"><a\n\
                            href="http://localhost/f.war/admincp/book/edit/1/1" title="Edit"\n\
                            class="icon-1 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/delete/1/1"\n\
                            title="Delete" class="icon-2 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/special/1/1"\n\
                            title="Special" class="icon-3 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/feature/1/1"\n\
                            title="Feature" class="icon-52 info-tooltip"></a></td>\n\
                    </tr>' % (row[0], row[1], row[3], row[2])
    previous = 1
    next = 1
    if (page == 1):
        previous = 1
    else:
        previous = page - 1
    if(page == numberofpage):
        next = page
    else:
        next = page + 1
    link = rootPath + '/backend/book/view/page01'
    link1 = rootPath + '/backend/book/view/page0%s' % (previous)
    link2 = rootPath + '/backend/book/view/page0%s' % (next)
    link3 = rootPath + '/backend/book/view/page0%s' % (numberofpage)
    var_book_paging = '<a href="%s"\n\
                        class="page-far-left"></a>\n\
                        <a href="%s"\n\
                        class="page-left"></a>\n\
                        <div id="page-info">\n\
                            Page <strong>%s</strong> / 11\n\
                        </div> <a href="%s"\n\
                        class="page-right"></a> <a\n\
                        href="%s"\n\
                        class="page-far-right"></a></td>\n\
                ' % (link, link1, page, link2, link3)
    
    
def view_book_filter(bName, cName):
    global var_book
    global var_book_head
    global var_book_paging
    var_book = ''
    var_book_head = ''
    var_book_paging = ''
    rows = ()
    if(bName == '' and cName == ''):
        rows = getAllBook()
    elif (cName == ''):
        rows = getBookFromName(bName)
    elif (bName == ''):
        rows = getBookFromCategory(cName)
    else:
        rows = getBookFromName(bName)
        rows += getBookFromCategory(cName)
    var_book_head = '<tr>\n\
                        <th class="table-header-check table-header-repeat"><a href="">ID</a>\n\
                        </th>\n\
                        <th class="table-header-repeat line-left minwidth-1"><a\n\
                            href="">Name</a></th>\n\
                        <th class="table-header-repeat line-left"><a href="">Category</a>\n\
                        </th>\n\
                        <th class="table-header-repeat line-left"><a href="">Created</a>\n\
                        </th>\n\
                        <th class="table-header-options line-left"><span\n\
                            class="none-link">Options</span>\n\
                        </th>\n\
                    </tr>\n\
                    '
    for row in rows:
        var_book += '<tr class="">\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td class="options-width"><a\n\
                            href="http://localhost/f.war/admincp/book/edit/1/1" title="Edit"\n\
                            class="icon-1 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/delete/1/1"\n\
                            title="Delete" class="icon-2 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/special/1/1"\n\
                            title="Special" class="icon-3 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/feature/1/1"\n\
                            title="Feature" class="icon-52 info-tooltip"></a></td>\n\
                    </tr>' % (row[0], row[1], row[3], row[2])
    
    
    
def back_book_page(request):
    if request.method == "POST":
        viewFilter()
        bName = request.POST['bname']
        cName = request.POST['category']
        view_book_filter(bName, cName)
        template = get_template('backend/bookpage.html')
        return return_request(template, request)
    else:
        return HttpResponseRedirect('page01')

def back_book_page_paging(request, page):
    viewFilter()
    currentpage = int(page)
    view_book(currentpage)
    template = get_template('backend/bookpage.html')
    return return_request(template, request)

def view_user(page):
    global var_user
    global var_user_paging
    var_user = ''
    var_user_paging = ''
    rows = getAllUser()
    len = 0
    for row in rows:
        len += 1
    numberofpage = int(math.ceil(len / 5.0))
    j = -1
    for i in range ((page - 1) * 5, page * 5):
        j += 1
        if i >= len:
            break
        row = rows[i]
        if (row[8] == 1):
            isadmin = 'Admin'
        else:
            isadmin = 'Normal'
        if j % 2 == 1:
            var_user += '<tr class="alternate-row">\n\
                        '
        else:
            var_user += '<tr class="">\n\
                    '
        var_user += '\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td><a href="mailto:%s">%s</a></td>\n\
                        <td>%s</td>\n\
                        <td class="options-width"><a href="../modify/1?numRows=5"\n\
                            class="icon-1 info-tooltip"></a></td>\n\
                    </tr>' % (row[0], row[1], row[4], row[4], isadmin)
    previous = 1
    next = 1
    if (page == 1):
        previous = 1
    else:
        previous = page - 1
    if(page == numberofpage):
        next = page
    else:
        next = page + 1
    link = rootPath + '/backend/user/view/page01'
    link1 = rootPath + '/backend/user/view/page0%s' % (previous)
    link2 = rootPath + '/backend/user/view/page0%s' % (next)
    link3 = rootPath + '/backend/user/view/page0%s' % (numberofpage)
    var_user_paging = '<a href="%s" class="page-far-left"></a> <a\n\
                    href="%s" class="page-left"></a>\n\
                    <div id="page-info">\n\
                        Page <strong><span id="page">%s</span> </strong> / <span\n\
                            id="numPages">%s</span>\n\
                    </div> <a href="%s" class="page-right"></a> <a\n\
                    href="%s" class="page-far-right"> </a>\n\
                ' % (link, link1, page, numberofpage, link2, link3)
def user_page(request):
    return HttpResponseRedirect('page01')

def user_page_paging(request, page):
    currentpage = int(page)
    view_user(currentpage)
    template = get_template('backend/userpage.html')
    return return_request(template, request)
    
    
def view_static():
    global var_static
    var_static = ''
    rows = getAllStatic()
    j = -1
    for row in rows:        
        j += 1
        if j % 2 == 1:
            var_static += '<tr class="alternate-row">\n\
                        '
        else:
            var_static += '<tr class="">\n\
                    '
        var_static += '\n\
                    <td>%s</td>\n\
                    <td>%s</td>\n\
                    <td>%s</td>\n\
                    <td class="options-width">\n\
                        <a href="%s/backend/static/edit/%s" title="Edit"    class="icon-1 info-tooltip"></a>\n\
                    </tr>' % (row[0], row[1], row[2], rootPath, row[0])
    
def view_setting():
    global var_setting
    var_setting = ''
    rows = getAllSetting()
    j = -1
    for row in rows:
        j += 1
        if j % 2 == 1:
            var_setting += '<tr class="alternate-row">\n\
                        '
        else:
            var_setting += '<tr class="">\n\
                    '
        var_setting += '\n\
                    <td>%s</td>\n\
                    <td>%s</td>\n\
                    <td>%s</td>\n\
                    <td class="options-width">\n\
                        <a href="%s/backend/setting/edit/%s" title="Edit"    class="icon-1 info-tooltip"></a>\n\
                    </tr>' % (row[0], row[1], row[2], rootPath, row[0])
    
def detail_category(cid):
    global var_book_category
    var_book_category = ''        
    rows = getBookOfCategory(cid)
    len = 0
    for row in rows:
        var_book_category += '<tr class="">\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td>%s</td>\n\
                        <td class="options-width"><a\n\
                            href="http://localhost/f.war/admincp/book/edit/1/1" title="Edit"\n\
                            class="icon-1 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/delete/1/1"\n\
                            title="Delete" class="icon-2 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/special/1/1"\n\
                            title="Special" class="icon-3 info-tooltip"></a> <a\n\
                            href="http://localhost/f.war/admincp/book/feature/1/1"\n\
                            title="Feature" class="icon-52 info-tooltip"></a></td>\n\
                    </tr>' % (row[0], row[1], row[3], row[2])  
     
def view_category_book(request, cid):
    detail_category(cid)
    template = get_template('backend/bookofcategory.html')
    return return_request(template, request)
    
def add_category_page(request):
    global var_check
    OldCategory = getAllCategory()
    if request.method == "POST":
        name = request.POST['name']
        weight = request.POST['weight']
        status = request.POST['status']
        kt = 0
        id = 0
        for i in OldCategory:
            if (i[1] == name): 
                kt = 0            
                break
            else:
                kt = 1
        if(kt == 1):
            addNewCategory(name, weight, status)        
            template = get_template('backend/addcategory.html')
            var_check = "<B><I>Insert Successfull !</I></B>"
            return return_request(template, request)
        else:
            var_check = "<B><I>Category Name already exists ! Choose another Name !</I></B>"            
    template = get_template('backend/addcategory.html')
    return return_request(template, request)

def check_loged(request):
    global loged
    user = request.user
    name = user.username
    if(name == ''):
        return False
    loged = name
    return True

def viewFilter ():
    global bookNameData
    global catNameData
    bookNameData = ''
    catNameData = ''
    bookNameData = getBookName()
    catNameData = getCatName()

def static_page(request):
    view_static()
    template = get_template('backend/staticpage.html')
    return return_request(template, request)
    
def static_edit_page(request, id):
    global var_integer
    global var_title_static 
    global var_detail_static
    if request.method == "POST":
        var_integer = id
        detail = request.POST['detail']
        submitEditStatic(id, detail)
        str = '%s/backend/static/view/' % (rootPath)
        return HttpResponseRedirect(str)
    else:    
        row = getDetailStatic(id)
        var_title_static = row[1]
        var_detail_static = row[0]
        template = get_template('backend/staticeditpage.html')
        return return_request(template, request)
    
def setting_page(request):
    view_setting()
    template = get_template('backend/settingpage.html')
    return return_request(template, request)
    
def setting_edit_page(request, id):
    global var_title_setting 
    global var_detail_setting
    if request.method == "POST":
        detail = request.POST['detail']
        submitEditSetting(id, detail)
        str = '%s/backend/setting/view/' % (rootPath)
        return HttpResponseRedirect(str)
    else:    
        row = getDetailSetting(id)
        var_title_setting = row[0]
        var_detail_setting = row[1]
        template = get_template('backend/settingeditpage.html')
        return return_request(template, request)    